package net.lab1024.sa.gateway.dto;

import org.springframework.util.MultiValueMap;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * HTTP 响应数据传输对象（DTO）
 * 封装响应状态码、头信息、响应体、耗时等核心属性
 */
public class HttpResponseDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    // 响应状态码（200=成功，400=参数错误，500=服务器异常等）
    private int statusCode;

    // 响应状态消息（如 "OK"、"Bad Request"）
    private String statusMessage;

    // 响应头（如 Content-Type、Set-Cookie 等）
    private MultiValueMap<String, String> headers;

    // 响应体（后端服务返回的业务数据，通常为 JSON 对象）
    private Object body;

    // 响应时间戳（网关接收响应的时间，用于监控耗时）
    private LocalDateTime timestamp;

    // 接口耗时（毫秒，从请求进入网关到接收响应的总时间）
    private long durationMs;

    // ==================================== 构造方法 ====================================

    /**
     * 默认构造方法
     */
    public HttpResponseDTO() {
        this.timestamp = LocalDateTime.now(); // 默认初始化时间戳
    }

    /**
     * 核心属性构造方法（常用于快速创建成功响应）
     * @param statusCode 状态码
     * @param body 响应体
     */
    public HttpResponseDTO(int statusCode, Object body) {
        this.statusCode = statusCode;
        this.body = body;
        this.timestamp = LocalDateTime.now();
    }

    /**
     * 全参构造方法
     */
    public HttpResponseDTO(int statusCode, String statusMessage, MultiValueMap<String, String> headers,
                           Object body, LocalDateTime timestamp, long durationMs) {
        this.statusCode = statusCode;
        this.statusMessage = statusMessage;
        this.headers = headers;
        this.body = body;
        this.timestamp = timestamp;
        this.durationMs = durationMs;
    }

    // ==================================== Getter/Setter ====================================
    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public MultiValueMap<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(MultiValueMap<String, String> headers) {
        this.headers = headers;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }

    public long getDurationMs() {
        return durationMs;
    }

    public void setDurationMs(long durationMs) {
        this.durationMs = durationMs;
    }
}